package com.zmj.sy.mom.srv.aps.mapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zmj.sy.mom.srv.aps.bean.entity.third.SinoCamReceiveNestPlate;
import com.zmj.sy.mom.srv.aps.bean.vo.report.ReportProductionNestPlateResVo;
import com.zmj.sy.mom.srv.aps.bean.vo.report.ReportProductionNestResVo;
import com.zmj.sy.mom.srv.aps.bean.vo.sinocam.SinoCamPlateListResVo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface SinoCamReceiveNestPlateMapper extends SyMapper<SinoCamReceiveNestPlate> {
    @Select(" select project_id, plate.stock_list_no, materiel_name , texture, norm, nest.nest_date nestDateStr, amount, " +
            " nest.rate, nest.width, nest.length " +
            " from tb_third_sinocam_receive_nest_plate plate  " +
            " left join tb_third_sinocam_receive_nest_nest nest on plate.receive_nest_id = nest.receive_nest_id " +
            " ${ew.customSqlSegment}")
    Page<SinoCamPlateListResVo> find(Page<SinoCamReceiveNestPlate> page, @Param(Constants.WRAPPER) QueryWrapper<SinoCamReceiveNestPlate> query);

    @Select(" select  nest2.report_pdf_url " +
            " from tb_third_sinocam_receive_nest_plate plate " +
            " left join tb_third_sinocam_receive_nest nest on plate.receive_nest_id = nest.id " +
            " left join tb_third_sinocam_receive_nest_nest nest2 on nest2.receive_nest_id = nest.id " +
            " where plate.stock_list_no = #{materCode}")
    String selectMapUrl(@Param("materCode") String materCode);

    @Select("select plate.* from tb_third_sinocam_receive_nest_plate plate where receive_nest_id in ( " +
            "  select distinct(receive_nest_id) from tb_third_sinocam_receive_nest_part part where part.nest_plan_id in ( " +
            "    select awd.id " +
            "    from tb_aps_work_detail awd " +
            "    left join tb_order o on awd.order_id = o.id " +
            "    where o.id = #{orderId} " +
            "  ) " +
            " ) ")
    List<SinoCamReceiveNestPlate> findPlateByOrder(@Param("orderId") Integer id);

    @Select(" select plate.id id, nest.id nestNestId, nest.nest_id nestId, plate.stock_list_no plateCode, ROUND(plate.rate ,2) rate, plate.png_url pngUrl, nest.option_id groupCode,  " +
            " plate.norm thickness, plate.texture texture, plate.length length,  plate.width width, plate.side_length sideLength,  nest.need_date needDateTime, nest.nester createUserCode,nest.nest_date createDateTime, nest.changer modifyUserCode,nest.change_date modifyDateTime, " +
            " pick.plate_state pickStatus, plate.heat_number heatCode,  " +
            " excess.ex_materiel_id,  " +
            " move1.cut_length cutLength, move2.cut_length emptyLength, p.nest_weight cutWeight,  " +
            " move1.total_cut_time cutTime,  " +
            " awd.plan_end_date_time deliveryDateTime,  " +
            " p.nest_lock nestLockStatus," +
            " nest.lantek_code pushType  " +
            " from tb_third_sinocam_receive_nest_plate plate    " +
            " left join tb_third_sinocam_receive_nest_nest nest on plate.nest_id = nest.nest_id   " +
            " left join tb_third_sinocam_receive_nest_excess excess on plate.stock_list_no = excess.stock_list_no  " +
            " left join tb_third_sinocam_receive_nest_move move1 on move1.nest_id = nest.nest_id and move1.move_type = 1 " +
            " left join tb_third_sinocam_receive_nest_move move2 on move2.nest_id = nest.nest_id and move2.move_type = 2 " +
            " left join tb_mes_plate_pick pick on pick.material_id = plate.stock_list_no " +
            " left join tb_mes_plate p on p.mater_code = plate.stock_list_no " +
            " left join tb_aps_work_detail awd on plate.stock_list_no = awd.mater_no and awd.phase_code in ('JG', 'HG') and awd.type = 1" +
            " ${ew.customSqlSegment} ")
    Page<ReportProductionNestResVo> productionNest( @Param(Constants.WRAPPER) QueryWrapper<SinoCamReceiveNestPlate> query, Page<SinoCamReceiveNestPlate> page);

    @Select(" select plate.id, plate.norm thickness, plate.texture spec, plate.stock_list_no plateCode, plate.length length, plate.width width, " +
            " plate.position_code positionCode, plate.sequence seq, plate.heat_number heatCode" +
            " from tb_third_sinocam_receive_nest_plate plate " +
            " ${ew.customSqlSegment} ")
    List<ReportProductionNestPlateResVo> productionNestPlate(@Param(Constants.WRAPPER) QueryWrapper<SinoCamReceiveNestPlate> query);

    @Select("DELETE FROM tb_third_sinocam_receive_nest_plate WHERE sino_cam_id=#{sId}")
    void deletePreOrder(@Param("sId") Integer sId);

    @Select("DELETE FROM tb_third_sinocam_receive_nest_plate WHERE id = #{id}")
    void deleteMesNest(@Param("id") Integer id);

    @Select("DELETE FROM tb_third_sinocam_receive_nest_plate WHERE stock_list_no = #{plateCode}")
    void deleteMesNestWithCode(@Param("plateCode") String plateCode);

}
